<HTML>
<HEAD>
<TITLE>[Chapter 14] 14.2 AudioClip Interface</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:54:01 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch14_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 14<br>And Then There Were Applets</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch14_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-14-SECT-2">14.2 AudioClip Interface</A></h2>

<P CLASS=para>
<A NAME="CH14.AUDIO"></A>Once an audio file is loaded into memory with <tt CLASS=literal>getAudioClip()</tt>, 
you use the <tt CLASS=literal>AudioClip</tt> interface 
to work with it. Methods

<P CLASS=para>
Three methods define the <tt CLASS=literal>AudioClip</tt> 
interface. The class that implements these methods depends on the run-time 
environment; the class is probably <tt CLASS=literal>sun.applet.AppletAudioClip</tt> 
or <tt CLASS=literal>netscape.applet.AppletAudioClip</tt>. 

<P CLASS=para>
If you play an audio clip anywhere within your <tt CLASS=literal>Applet</tt>, 
you should call the <tt CLASS=literal>AudioClip</tt> 
<tt CLASS=literal>stop()</tt> method within the <tt CLASS=literal>stop()</tt> 
method of the applet. This ensures that the audio file will stop playing 
when the user leaves your web page. Stopping audio clips is a must if you 
call <tt CLASS=literal>loop()</tt> to play the sound 
continuously; if you don't stop an audio clip, the user will have to exit the browser 
to get the sound to stop playing. 

<P CLASS=para>
Applets can play audio clips simultaneously. Based upon the user's actions, you may want to play a sound 
file in the background continuously, while playing other files.

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>void play () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>play()</tt> method plays the 
audio clip once from the beginning. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>void loop () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>loop()</tt> method plays the 
audio clip continuously. When it gets to the end-of-file marker, it resets 
itself to the beginning. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>void stop () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>stop()</tt> method stops the 
applet from playing the audio clip. </DL>
<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-14-SECT-2.1">Using an AudioClip</A></h3>

<P CLASS=para>
The applet in <A HREF="ch14_02.htm#JAWT-CH-14-EX-2">Example 14.2</A> loads three audio files in the <tt CLASS=literal>init()</tt> 
method. The <tt CLASS=literal>start()</tt> method 
plays Dino barking in the background as a continuous loop. Whenever the 
browser calls <tt CLASS=literal>paint()</tt>, Fred 
yells "Wilma," and when you click the mouse anywhere, the call 
to <tt CLASS=literal>mouseDown()</tt> plays Fred yelling, 
"Yabba-Dabba-Doo." If you try real hard, all three can play 
at once. Before playing any audio clip, the applet makes sure that the 
clip is not null--that is, that the clip loaded correctly. <tt CLASS=literal>stop()</tt> 
stops all clips from playing; you should make sure that applets stop all 
audio clips before the viewer leaves the web page. 

<DIV CLASS=example>
<h4 CLASS=example><A CLASS="TITLE" NAME="JAWT-CH-14-EX-2">Example 14.2: AudioClip Usage</A></h4>

<DIV CLASS=screen>
<P>
<PRE>
import java.net.*;
import java.awt.*;
import java.applet.*;
public class AudioTestExample extends Applet{
    AudioClip audio1, audio2, audio3;
    public void init () {
        audio1 = getAudioClip (getCodeBase(), "audio/flintstones.au");
        audio2 = getAudioClip (getCodeBase(), "audio/dino.au");
        audio3 = getAudioClip (getCodeBase(), "audio/wilma.au");
    }
    public boolean mouseDown (Event e, int x, int y) {
        if (audio1 != null)
            audio1.play();
        return true;
    }
    public void start () {
        if (audio2 != null)
            audio2.loop();
    }
    public void paint (Graphics g) {
        if (audio3 != null)
            audio3.play();
    }
    public void stop () {
        if (audio1 != null)
            audio1.stop();
        if (audio2 != null)
            audio2.stop();
        if (audio3 != null)
            audio3.stop();
    }
}
</PRE>
</DIV>

</DIV>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch14_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch14_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>What's a Java Applet?</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>AppletContext Interface</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
